package com.sgb.member.ui.activity;

import android.app.Application;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.member.MemberConfig;
import com.sgb.member.entity.ApplyCompanyBean;
import com.sgb.member.entity.CompanyDeptBean;
import com.sgb.member.entity.MemberApprovalBean;
import com.sgb.member.entity.UserMsgEntity;
import com.sgb.member.service.MemberAPI;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.SharedPreferenceUtil;

import java.util.List;
import java.util.Map;

import retrofit2.Call;

// 会员审批信息填写
public class MemberApprovalModel extends AndroidViewModel {
    public MutableLiveData<Boolean> dialog = new MutableLiveData<>();
    public MutableLiveData<List<ApplyCompanyBean>> mCompanyList = new MutableLiveData<>();
    public MutableLiveData<List<CompanyDeptBean>> mDepList = new MutableLiveData<>();
    public MutableLiveData<BaseEntity> mApproveSuccess = new MutableLiveData<>();
    public MutableLiveData<String> mApprovalStatus = new MutableLiveData<>();

    public MutableLiveData<String> mCompanyH5= new MutableLiveData<>();
    private MemberAPI api;
    public MemberApprovalModel(@NonNull Application application) {
        super(application);
        api = MemberConfig.getInstance().getApi();
    }

    /**
     * 获取审批流程状态
     *
     * @param approveType 邀请加盟审批类型 inviteJoinCompany
     *                    申請加盟审批类型 applyJoinCompany
     *                    会员企业调级 memberEvaluate
     *                    会员解除申请 memberRelieve
     */
    public void getProcessStatus(String companyNo, String approveType) {
        dialog.setValue(true);
        api.getProcessStatus(companyNo, approveType).enqueue(new ICallBack<BaseEntity<Object>>() {
            @Override
            public void onSuccess(BaseEntity<Object> data) {
                // "status": "1" //1 表示有审批 0 代表无审批
                String status = ((Map<String, String>) data.getData()).get("status");
                mApprovalStatus.setValue(status);
            }

            @Override
            public void onFailure(Call<BaseEntity<Object>> call, Throwable t) {
                super.onFailure(call, t);
                mApprovalStatus.setValue("error");
            }
        });
    }

    public void sendApprove(MemberApprovalBean bean) {
        dialog.setValue(true);
        api.sendApprove(bean).enqueue(new ICallBack<BaseEntity<BaseEntity>>() {
            @Override
            public void onSuccess(BaseEntity<BaseEntity> data) {
                mApproveSuccess.setValue(data.getData());
            }
        });
    }

    // 审批【申请】企业列表
    public void getMyCompanyList() {
        dialog.setValue(true);
        api.getMyCompanyList().enqueue(new ICallBack<BaseEntity<List<ApplyCompanyBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<ApplyCompanyBean>> data) {
                mCompanyList.setValue(data.getData());
            }
        });
    }

    // 审批【申请】部门列表
    public void getDeptList(String companyId) {
        dialog.setValue(true);
        api.getDeptList(companyId).enqueue(new ICallBack<BaseEntity<List<CompanyDeptBean>>>() {
            @Override
            public void onSuccess(BaseEntity<List<CompanyDeptBean>> data) {
                mDepList.setValue(data.getData());
            }
        });
    }


    public MutableLiveData<Boolean> userInfo = new MutableLiveData<>();

    public void getUserInfo() {
        dialog.setValue(true);
        api.getUserInfo().enqueue(new ICallBack<BaseEntity<UserMsgEntity>>() {
            @Override
            public void onSuccess(BaseEntity<UserMsgEntity> data) {
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NAME, data.getData().getUserName());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.USER_NO, data.getData().getNo());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_NAME, data.getData().getRealName());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.DRIVER_AUTH, data.getData().getIsDriverAuth());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.ENTERPRISE_AUTH, data.getData().getEnterpriseAuth());
                if (data.getData().getEnterprise() == null) {
                    userInfo.setValue(false);
                    return;
                }
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NAME, data.getData().getEnterprise().getCompanyName());
                Log.e("公司no", data.getData().getEnterprise().getNo());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.COMPANY_NO, data.getData().getEnterprise().getNo());
                SharedPreferenceUtil.getInstance().saveData(SharedPreferenceUtil.REAL_ID, data.getData().getEnterprise().getId());
                userInfo.setValue(true);
            }

            @Override
            public void onFailure(Call<BaseEntity<UserMsgEntity>> call, Throwable t) {
                super.onFailure(call, t);
                userInfo.setValue(false);
            }
        });
    }

    //获取h5链接
    public void companyInfoDetail(String companyId) {
        dialog.setValue(true);
        api.getUrlSetting("companyInfoDetail").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                String url = data.getData().getUrl() + "?id=" + companyId;// isExamine从会员审核列表进入传1
//                WebViewX5HostActivity.start(activity, url, 101);
                mCompanyH5.setValue(url);
            }
        });
    }
}
